# self 代表当前对象，而不是类
# self不必非要写成self
# 约定俗成都使用self
# class User():
#     name="sc"
#     def infO(self):
#         print(f"I am {self.name}")
#         print(self)
#         print(self.__class__)
# user1=User()
# user1.infO()
# user2=User()
# user2.infO()
# #self可以不写,就不能用实例传参数，只能用类进行传递
# class User():
#     name="sc"
#     def info(self):
#         print(f"I am {self.name}")
#         print(self)
#         print(self.__class__)
#     def info2():
#         print("this is info2")
# user3=User()
# user3.info()   ####===>User.info(user3)
# # user3.info2()   ####===>User.info2(user3)  ×
# User.info2()

# 用户类
#属性： 名字，密码，余额，
# 行为
# 进行购物，——购买什么，购买多少——》余额减少
# 商品类
# 属性：名字，类别，价格，库存（斤）
# 行为：
# 库存减少
# class User():
#     # name=None
#     # instance = {}
#     # paaswd_='123456'
#     # yu_e=500
#     # def __new__(cls, *args, **kwargs):
#     #     if cls.instance=={}:
#     #         cls.instance=object.__new__(cls)
#     #     return cls.instance
#     def __init__(self,name,passwd,yu_e):
#         self.name=name
#         self.passwd=passwd
#         self.yu_e=yu_e
#         User.instance[name]=[passwd,yu_e]
#     def shopping(self,name,passwd):
#         if passwd == User.paaswd_:
#             print(f"您的用户名为{name},余额为{User.yu_e}")
#
# class Goods():
#     # type_goods='fruit'
#     # name_goods={'apple':10,'香蕉':5,'西瓜':7,'梨子':9}
#     # # price_goods=10
#     # left_goods={'apple':15,'香蕉':10,'西瓜':30,'梨子':30}
#     def __init__(self,classify,product_name,price,store):
#         self.classify=classify
#         self.product_name=product_name
#         self.price=price
#         self.store=store
#
#     def buy_goods(self,what,num):
#         total=Goods.name_goods[what]*num
#         Goods.left_goods[what]-=num
#         print(f"一共消费{total}元")
#         return total
#
# user=User(name,passwd)
# user.shopping(name,passwd)
# user1=Goods()
# print(f"我们有这些水果:\n{user1.name_goods}")
# buy_fruit=input("请输入你要买的水果：")
# num_fruit=int(input("请输入你要买的数量"))
# buy=user1.buy_goods(buy_fruit,num_fruit)
# User.yu_e-=buy
# print(f"您的余额为{User.yu_e}")
# 用户类
#属性： 名字，密码，余额，
# 行为
# 进行购物，——购买什么，购买多少——》余额减少
# 商品类
# 属性：名字，类别，价格，库存（斤）
# 行为：
# 库存减少
class User():
    def __init__(self):
        self.usr_info = {"jack":["123",500], "alex": ["456",500]}
    def register(self):
        print("注册模式：")
        usr=input("请输入用户名：")
        pwd=input("请输入密码：")
        pwd2=input("请再一次输入密码：")
        money=input("请输入您的初始金额：")
        if pwd==pwd2:
            if usr in self.usr_info.keys():
                print("用户名已注册")
                self.register()
            else:
                self.usr_info[usr]=[pwd,money]
                print("注册成功")
                x = input("是否使用新用户名密码登录?登录请输入1，退出请输入2：")
                if x == "1":
                    self.login()
                else:
                    print("退出系统，欢迎下次使用。")
                    return
        else:
            print("两次密码不一致，请重新注册")
            self.register()
    def login(self):
        print("登录模式")
        for i in range(3):
            usr=input("请输入用户名：")
            pwd=input("请输入密码：")
            if usr in self.usr_info.keys():
                if pwd == self.usr_info[usr][0]:
                    print("登录成功")
                    self.shopping(usr,self.usr_info[usr][1])
                else:
                    print("密码输入错误，请重新输入")
            else:
                print("用户不存在，请注册")
            print(f"您已经输入错误{i+1}遍，你还剩下{2-i}次机会")
        else:
            print("您的机会已经用完，登录失败")


    def shoppinglis(self):
        self.list={"jack":{"apple":3,"orange":5}}

    def shopping(self,usr,yu_e):
        self.shoplist = {'F001': {'name': '苹果', 'price': '1.2'},
                         'F002': {'name': '香蕉', 'price': '5.5'},
                         'F003': {'name': '砂糖橘', 'price': '3.1'},
                         'F004': {'name': '柚子', 'price': '10.5'},
                         'F005': {'name': '奥迪A8', 'price': '888888'}
                         }
        print(""""
            #######################################################################
            =========================/今日商品信息/=================================
                               {'F001':{'name':'苹果'，'price':'1.2'}}
                               {'F002':{'name':'香蕉'，'price':'5.5'}}
                               {'F003':{'name':'砂糖橘'，'price':'3.1'}}
                               {'F004':{'name':'柚子'，'price':'10.5'}}
                               {'F005':{'name':'AuDi A8 '，'price':'888888.8'}}
            """)
        print("输入exit可以退出系统")
        print("按r结账")
        sumprice=0
        buylist={}

        while True:
            shopinput = input("请输入你要购买的商品编号：")
            if shopinput == 'exit':
                exit()
            else:
                if shopinput in self.shoplist.keys():
                    sumprice += float(self.shoplist[shopinput]['price'])
                    buylist.update({self.shoplist[shopinput]['name']:
                                        {'单价': self.shoplist[shopinput]['price'],
                                         '总价': sumprice}})
                    print(f"我的购物车:{buylist}")
                    # 输出每一次购物信息
                    print(f"购买信息：{self.shoplist[shopinput]['name']},单价："
                          f"{self.shoplist[shopinput]['price']}")
                elif shopinput == 'r' or shopinput == 'R':
                    print("现在开始结算。")
                    print("您今天的购物信息：")
                    print(buylist)
                    print("需要支付：", sumprice)
                    outmoney = int(yu_e)
                    if outmoney >= sumprice:
                        outmoney -= sumprice
                        print("当前账户余额：", outmoney)
                        return exit()
                    else:
                        print("您卡里余额不足")
                else:
                    print("输入错误！")

        # if weight > goods.store:
        #     print("库存不足")
        # else:
        #     pay=weight*goods.price
        #     if self.yu_e >= pay:
        #         self.yu_e-=pay
        #         goods.store-=weight
        #         print(f"成功购买,{self.name}剩余{self.yu_e}元"\
        #         f"{goods.product_name}剩余库存{goods.store}")
        #     else:
        #         print("余额不足")

class Goods():
    def __init__(self):
        # self.classify=classify
        # self.product_name=product_name
        # self.price=price
        # self.store=store
        self.shoplist = {'F001': {'name': '苹果', 'price': '1.2'},
                    'F002': {'name': '香蕉', 'price': '5.5'},
                    'F003': {'name': '砂糖橘', 'price': '3.1'},
                    'F004': {'name': '柚子', 'price': '10.5'},
                    'F005': {'name': '奥迪A8', 'price': '888888'}
                    }
# user1 = User("zhangjian","123456",5000)
# user2 = User("wangshenghu","1234567",8000)
#
# apple = Goods("fruits","apple",8,30)
# orange = Goods("fruits","orange",4,100)
#
# user1.shopping(apple,3)
user = User()    # 创建user对象
print("欢迎使用三乐购物系统-v1.0".center(100, "-"))
print("注册请输入0,登录请输入1,退出请输入2".center(95, "-"))
select = input("请选择您需要进行的操作：")
if select=="0":
    user.register()
elif select=="1":
    user.login()
    print("购买请按1，推出请按2")
    y = input("请输入你的选择：")
    if y == "1":
        user.shopping()
    else:
        user.login()
elif select=="2":
    print("退出系统！欢迎再次使用。")
else:
    print("输入有误，请正确输入")

